# -*- coding = utf-8 -*-
# @Time : 2023/5/19 13:18
# @Author :Henry L
# @File : CQ.py
import glob
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


class SD:
    def __init__(self):
        options = webdriver.ChromeOptions()
        self.host = "https://nxyp.nxggzyjy.org/"
        prefs = {"plugins.always_open_pdf_externally": True,
                 "download.default_directory": os.getcwd() + "\\"}
        options.add_experimental_option("prefs", prefs)
        self.url = 'https://nxyp.nxggzyjy.org/cms/showListZCFG.html?catalogId=2'
        self.driver = webdriver.Chrome(options=options)
        self.driver.maximize_window()
        self.wait = WebDriverWait(driver=self.driver, timeout=10)
        self.driver.get(self.url)

    def get(self, page):
        time.sleep(3)
        # tbody = self.driver.find_element(By.XPATH, "/html/body/div[6]/div[2]/div/div[3]/table/tbody")
        lan = self.driver.find_elements(By.XPATH, "/html/body/div[2]/div[2]/div/div/ul/li/a/label")
        date_list = self.driver.find_elements(By.XPATH, "/html/body/div[2]/div[2]/div/div/ul/li/span")
        for item in range(0, len(lan)):
            time.sleep(3)
            # if item.find_element(By.XPATH, "div[1]/div[2]").text == '公告标题':
            #     continue
            date = date_list[item].text
            lan[item].click()
            # handles = self.driver.window_handles
            # self.driver.switch_to.window(handles[-1])
            time.sleep(1.5)
            title = self.driver.find_element(By.CLASS_NAME, "contentTitle").get_attribute("innerText")
            box = self.driver.find_element(By.CLASS_NAME, "contentCon")
            view_time = ''
            content = box.get_attribute("outerHTML")
            attachments = box.find_elements(By.TAG_NAME, "a")
            attachments_list = []
            if len(attachments) > 0:
                for a in attachments:
                    if a.get_attribute("innerText") == '' or a.get_attribute("href") is None:
                        continue
                    if "attachment" in a.get_attribute("href") or "附件" in a.text:
                        self.driver.execute_script("window.scroll(0,1000)")
                        a.click()
                        time.sleep(1)
                        # href = a.get_attribute("href").split('.')[0]
                        # a_name = href.split('/')[-1]
                        # attachments_list.append(a_name)
                        # self.get_last_filename_and_rename(os.getcwd(), a_name)
            time.sleep(1)
            while self.driver.current_url != 'https://nxyp.nxggzyjy.org/cms/showListZCFG.html?catalogId=2':
                self.driver.back()
                time.sleep(1)
            data_list = [title, date, view_time, content, attachments_list]
            print(data_list)

    def get_last_filename_and_rename(self, sav_folder, new_filename):
        files = glob.glob(sav_folder + '/*')
        max_file = max(files, key=os.path.getctime)
        filename = max_file.split("/")[-1].split(".")[0]
        new_path = max_file.replace(filename, new_filename)
        os.rename(max_file, new_path)
        return new_path

    def close(self):
        time.sleep(60)
        self.driver.quit()

sd = SD()
sd.get("")
sd.close()
